(function(app){
	app
		.controller("taxInfoCtrl",["$scope","myFactory",'$http','$stateParams','$state',"$q",'$timeout','$location','$ionicHistory',function($scope,myFactory,$http,$stateParams,$state,$q,$timeout,$location,$ionicHistory){
			//接收参数
			var itemId = $stateParams.itemId;//变更项目ID
			var item = $stateParams.item;
			var chdrnum =  $stateParams.chdrnum; //保单号
			var chdrcoy = $stateParams.chdrcoy; //保单公司号码
			var searchParam = $stateParams.searchParam;
			var changeFlag = $stateParams.isChangeOtherPolicy;//是否变更名下其他保单
			var taxBenefitRole = $stateParams.taxBenefitRole;
			var taxClientNo = $stateParams.taxClientNo;
			$scope.taxName = $stateParams.taxName;
			var taxData;//保单信息
			var initFlag = false;//检测初始化是否完成标志
			var token=sessionStorage.getItem("token");
			$scope.reason1_checkFlag = false;
			$scope.reason1_checkVal = "";
			$scope.reason2_checkVal = "";
			myFactory.initDateUtil('birth_day','');//设值打开显示控件当前日期
			$("#residentContent").hide();
			
			//初始化查询变更项内容
			$scope.load = function () {
				myFactory.loadingShow();//显示加载圈
				var timestamp=myFactory.getTimetamp();
				var sfis= "itemId-token-timestamp";
				var signStr="itemId="+itemId+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				$http({
					method: 'GET',
					url: app.domain + 'query/getTaxInfo.action',
					params: {'itemId': itemId,'chdrnum':chdrnum,"itemStr":item,"taxBenefitRole":taxBenefitRole,"taxClientNo":taxClientNo,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						$scope.taxData=response.data.data.taxData;
						taxData = response.data.data.taxData;
						if(taxData!=null){
							//设置单选按钮的选项
							if(taxData.pflag!=null && taxData.pflag!= ""){
								var pflag = taxData.pflag;
								$("#choose_resident"+pflag).attr("checked","checked");
								
								if("1"==pflag){
									$("#residentContent").hide();
								}else{
									$("#residentContent").show();
									//设置出生日期
									var birthday=myFactory.DateTools(taxData.smoddate);
									$scope.birthday=birthday;
									console.log("birthday:"+birthday);
									//出生日期
									myFactory.initDateUtil('birth_day',birthday);
									
									//设置未纳税人税号的原因
									if(taxData.reasons != null && taxData.reasons != ""){
										$("#resident_reson"+taxData.reasons).attr("checked","checked");
										
										if("2"==taxData.reasons){//勾选了账户持有人未能取得纳税人识别号选项
											$scope.reason2_checkFlag = true;
											$scope.reason2_checkVal = "2";
											$("#pareason").val(taxData.pareason);
											$("#resident_reson").slideDown();
										}else if("1"==taxData.reasons){
											$scope.reason1_checkFlag = true;
											$scope.reason1_checkVal = "2";
											$("#resident_reson").slideUp();
										}
									}
								}
							}
						}
						initFlag = true;
					}
					myFactory.loadingHide();
				}, function errorCallback(response) {
					myFactory.loadingHide();
					myFactory.alert("","初始化查询变更项内容失败!","info");
				});
			}
			
			$scope.data = {
				checkFlag: false
			},
			//重新受理
			$scope.renew = function(){
				myFactory.renew();
			}
			//退出移动保全
			$scope.exit = function(){
				myFactory.exit();
			}
			
			//居民国（地区）纳税人选择
			$scope.chooseResidentChinaYes = function(){
				$("#residentContent").hide();
			}
			$scope.chooseResidentChinaNo = function(){
				$("#residentContent").show();
			}
			
			//业务：这里非单选框逻辑，复选框，但是允许不选，同时又不允许多选
			//不能提供居民国（地区）纳税人识别号原因
			$scope.residentResonYes = function(){
				$scope.reason1_checkFlag = !$scope.reason1_checkFlag;
				if ($scope.reason1_checkFlag) {
					$scope.reason1_checkVal = "2";
					$scope.reason2_checkVal = "1";
					$scope.reason2_checkFlag=false;
					$("#resident_reson1").attr("checked","checked");
					$("#resident_reson2").attr("checked",false);
					$("#pareason").val("");
				} else{
					$scope.reason1_checkVal = "1";
				}
				console.log("reason1_checkFlag:"+$scope.reason1_checkFlag+",reason1_checkVal:"+$scope.reason1_checkVal);
				$("#resident_reson").slideUp();//收缩
			}
			$scope.residentResonNo = function(val){
				$scope.reason2_checkFlag = !$scope.reason2_checkFlag;
				if ($scope.reason2_checkFlag) {
					$scope.reason2_checkVal = "2";
					$scope.reason1_checkVal = "1";
					$scope.reason1_checkFlag=false;
					$("#resident_reson2").attr("checked","checked");
					$("#resident_reson1").attr("checked",false);
					$("#resident_reson").slideDown();//展开
				} else{
					$scope.reason2_checkVal = "1";
					$("#pareason").val("");
					$("#resident_reson").slideUp();//收缩
				}
				console.log("reason2_checkFlag:"+$scope.reason2_checkFlag+",reason2_checkVal:"+$scope.reason2_checkVal);
			}
			
			//客户须知
			$scope.tipOpen = function(){
				modal('open','tip_model','overwindow');
			}
			$scope.closeTip = function(){
				modal('close','tip_model','overwindow');
			}
			
			//下一步
			$scope.next = function(){
				//检验必填项
				if(isNaN($("input[name='choose_resident']:checked").val())){
					$.alert({title: "",text: "请选择纳税人身份！",onOK: function () {}});
					return false;
				}else{
					var pflag=$("input[name='choose_resident']:checked").val();
					if(pflag==1){
						taxData["pflag"]=pflag;
					}else{
						if( isNaN($("#susrname").val())){
							$.alert({title: "",text: "'姓（英文或拼音）' 不能为空！",onOK: function () {}});
							return false;
						}
						if(isNaN($("#susrgname").val())){
							$.alert({title: "",text: "'名（英文或拼音）' 不能为空！",onOK: function () {}});
							return false;
						}
						if(isNaN($("#birth_day").val())){
							$.alert({title: "",text: "'出生日期' 不能为空！",onOK: function () {}});
							return false;
						}
						
						if((isNaN($("#nationdc").val()) && isNaN($("#sprovname").val()) && isNaN( $("#scityname").val()) && isNaN($("#lngaddr01").val()))
								&&(isNaN( $("#nationabdc").val()) && isNaN($("#pprovname").val()) && isNaN($("#pcityname").val()) && isNaN($("#lngaddr02").val()))){
							myFactory.alert("","'现居地址（中文）','现居地址（英文或拼音）'须任填一项，请检查！","info");
							return false;	
						}
						
						if(!isNaN($("#nationdc").val()) || !isNaN($("#sprovname").val()) || !isNaN( $("#scityname").val()) || !isNaN($("#lngaddr01").val())){
							if(isNaN($("#nationdc").val()) || isNaN($("#sprovname").val()) || isNaN( $("#scityname").val()) || isNaN($("#lngaddr01").val())){
								myFactory.alert("","现居地址（中文）填写不完整。","info");
								return false;	
							}	
						}
						
						if(!isNaN( $("#nationabdc").val()) || !isNaN($("#pprovname").val()) || !isNaN($("#pcityname").val()) || !isNaN($("#lngaddr02").val())){
							if(isNaN( $("#nationabdc").val()) || isNaN($("#pprovname").val()) || isNaN($("#pcityname").val()) || isNaN($("#lngaddr02").val())){
								myFactory.alert("","现居地址（英文或拼音）填写不完整。","info");
								return false;	
							}
						}
						
						if((isNaN($("#cntname").val()) && isNaN( $("#provname").val()) && isNaN($("#tax_city").val()) && isNaN($("#lngaddr03").val()))
								&&(isNaN($("#crname").val()) && isNaN($("#prodesc").val()) && isNaN($("#citydesc").val()) && isNaN($("#lngaddr04").val()))){
							myFactory.alert("","'出生地（中文）' , '出生地（英文或拼音）'须任填一项，请检查！","info");
							return false;	
						}
						
						
						if(!isNaN($("#cntname").val()) || !isNaN( $("#provname").val()) || !isNaN($("#tax_city").val()) || !isNaN($("#lngaddr03").val())){
							if(isNaN($("#cntname").val()) || isNaN( $("#provname").val()) || isNaN($("#tax_city").val()) || isNaN($("#lngaddr03").val())){
								myFactory.alert("","出生地（中文）填写不完整。","info");
								return false;	
							}
						}
						
						if(!isNaN($("#crname").val()) || !isNaN( $("#prodesc").val()) || !isNaN($("#citydesc").val()) || !isNaN($("#lngaddr04").val())){
							if(isNaN($("#crname").val()) || isNaN($("#prodesc").val()) || isNaN($("#citydesc").val()) || isNaN($("#lngaddr04").val())){
								myFactory.alert("","出生地（英文或拼音）填写不完整。","info");
								return false;	
							}	
						}
						
						if((isNaN( $("#sdesc01").val()) && isNaN($("#pedesc01").val()))
								&&(isNaN( $("#sdesc02").val()) && isNaN($("#pedesc02").val()))
								&&(isNaN( $("#sdesc03").val()) && isNaN($("#pedesc03").val()))){
							 var reasonFlag = $("input[name='resident_reson']:checked").val();
							 if(isNaN(reasonFlag)){
								 $.alert({title: "",text: "税收居民国(地区)及纳税人识别号未填，请选择原因！",onOK: function () {}});
									return false;	
							 }else{
								 if(reasonFlag==2&&isNaN($("#pareason").val())){
									 $.alert({title: "",text: "请填写解释原因！",onOK: function () {}});
										return false;	
								 }
							 }
						}
						
						if((!isNaN( $("#sdesc01").val()) && !isNaN($("#pedesc01").val()))
								|| (!isNaN( $("#sdesc02").val()) && !isNaN($("#pedesc02").val()))
								|| (!isNaN( $("#sdesc03").val()) && !isNaN($("#pedesc03").val()))){
							 var reasonFlag = $("input[name='resident_reson']:checked").val();
							 if(!isNaN(reasonFlag)){
								 $.alert({title: "",text: "已填写纳税人识别号，无须勾选原因，请确认！",onOK: function () {}});
									return false;	
							 }
						}
						
						//变更之后数据绑定
						taxData["pflag"]=pflag;
						taxData["susrname"]= $("#susrname").val();
						taxData["susrgname"]= $("#susrgname").val();
						var birth_day=$("#birth_day").val();
						birth_day=birth_day.replace(/-/g, "");
						taxData["smoddate"]= birth_day;
						taxData["nationdc"]= $("#nationdc").val();
						taxData["sprovname"]= $("#sprovname").val();
						taxData["scityname"]= $("#scityname").val();
						taxData["lngaddr01"]= $("#lngaddr01").val();
						taxData["nationabdc"]= $("#nationabdc").val();
						taxData["pprovname"]= $("#pprovname").val();
						taxData["pcityname"]= $("#pcityname").val();
						taxData["lngaddr02"]= $("#lngaddr02").val();
						taxData["cntname"]= $("#cntname").val();
						taxData["provname"]= $("#provname").val();
						taxData["city"]= $("#tax_city").val();
						taxData["lngaddr03"]= $("#lngaddr03").val();
						taxData["crname"]= $("#crname").val();
						taxData["prodesc"]= $("#prodesc").val();
						taxData["citydesc"]= $("#citydesc").val();
						taxData["lngaddr04"]= $("#lngaddr04").val();
						taxData["sdesc01"]= $("#sdesc01").val();
						taxData["pedesc01"]= $("#pedesc01").val();
						taxData["sdesc02"]= $("#sdesc02").val();
						taxData["pedesc02"]= $("#pedesc02").val();
						taxData["sdesc03"]= $("#sdesc03").val();
						taxData["pedesc03"]= $("#pedesc03").val();
						taxData["reasons"]= $("input[name='resident_reson']:checked").val();
						taxData["pareason"]= $("#pareason").val();
					}
				}
				
				myFactory.loadingShow();//显示加载圈
				var timestamp=myFactory.getTimetamp();
				var sfis= "token-timestamp";
				var signStr="token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//将变更后的个人税收加入缓存当中
				$http({
					method: 'GET',
					url: app.domain + 'input/saveTaxInfo.action',
					params: {"taxStr":taxData,"taxBenefitRole":taxBenefitRole,"taxClientNo":taxClientNo,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {

						//CRS个人税收居民身份保全需求 增加控制填写投保人、受益人单次填写问题 add by rqyang 2018-12-08 begin
						if("bf"==taxBenefitRole){
							sessionStorage.setItem("BENFIT_CRS_ISEXISTS_FLAG", "Y");
							console.log("CRS个人税收居民身份受益人信息已填写完成-BENFIT_CRS_ISEXISTS_FLAG=Y");
						}else{
							sessionStorage.setItem("HOLDER_CRS_ISEXISTS_FLAG", "Y");
							console.log("CRS个人税收居民身份投保人信息已填写完成-HOLDER_CRS_ISEXISTS_FLAG=Y");
						}
						//CRS个人税收居民身份保全需求 增加控制填写投保人、受益人单次填写问题 add by rqyang 2018-12-08 end
						
						if(changeFlag!=null&&changeFlag!=""){
							if(changeFlag){//选择是
								item["changeOther"] = "Y";
								$state.go('content_01_list',{
									itemId:'01',item:item,chdrnum01:$scope.chdrnum,chdrcoy:chdrcoy
								});
							}else{//选择否
								item["changeOther"] = "N";
								$state.go('content_check_main',{
									itemId:itemId,
									item:item,
									chdrnum01:chdrnum,
									chdrcoy:chdrcoy
								});
							}
						}else{
							$state.go('content_check_main',{
								itemId:itemId,
								item:item,
								chdrnum01:chdrnum,
								chdrcoy:chdrcoy
							});
						}
						
						//console.log("-----个人税收资料数据加入缓存成功-----");
					}
				}, function errorCallback(response) {
					//console.log("-----个人税收资料数据加入缓存失败----");
				});
				myFactory.loadingHide();
			}
			//返回上一页
			$scope.goBack = function(){
				/*var timestamp=myFactory.getTimetamp();
				var sfis= "token-timestamp";
				var signStr="token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				$http({
					method: 'GET',
					url: app.domain + 'input/removeTaxInfoCache.action',
					params: {"taxStr":taxData,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					myFactory.goBack();
				},function errorCallback(response) {
					//console.log("-----个人税收资料数据加入缓存失败----");
				});*/
				
				//CRS个人税收居民身份保全需求 增加控制填写投保人、受益人单次填写问题 add by rqyang 2018-12-08 begin
				$.confirm("返回将清除已填写个人税收居民身份,是否返回上一页？", "", function() {
					if("bf"==taxBenefitRole){
						sessionStorage.setItem("BENFIT_CRS_ISEXISTS_FLAG", "N");
						console.log("CRS个人税收居民身份受益人信息已填写内容返回上一页提示清除-BENFIT_CRS_ISEXISTS_FLAG=N");
					}else{
						sessionStorage.setItem("HOLDER_CRS_ISEXISTS_FLAG", "N");
						console.log("CRS个人税收居民身份投保人信息已填写内容返回上一页提示清除-BENFIT_CRS_ISEXISTS_FLAG=N");
					}
					//后台服务缓存清除
					$scope.clearCrs(taxBenefitRole);
					myFactory.goBack();
				});
				//CRS个人税收居民身份保全需求 增加控制填写投保人、受益人单次填写问题 add by rqyang 2018-12-08 end
			}	
			$scope.clearCrs = function(taxRoleFlag){
				var timestamp=myFactory.getTimetamp();
				var sfis= "token-timestamp";
				var signStr="token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				$http({
					method: 'GET',
					url: app.domain + 'input/removeTaxInfoCache.action',
					params: {"taxRoleFlag":taxRoleFlag,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					console.log("-----个人税收资料数据清除缓存成功----");
				},function errorCallback(response) {
					console.log("-----个人税收资料数据清除缓存失败----");
				});
			}	
			//判断是否为空
			function isNaN(data){
				if(data==null || data=="" || data==undefined){
					return true;
				}else if(data.trim()==""){
					return true;
				}
				return false;
			}
		}]);        
})(app)